package demo.xiaxl.com.android_demo.pagersnaphelper;

import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import demo.xiaxl.com.android_demo.R;


/**
 *
 */
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {

    // 数据集
    private ArrayList<DataBean> mDataList;


    private int mWidth;
    private int mHeight;

    //
    public ContentAdapter(ArrayList<DataBean> dataset, int width, int height) {
        super();
        this.mDataList = dataset;
        mWidth = width;
        mHeight = height;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        Log.e("xiaxl: ", "---onCreateViewHolder---");
        // 创建一个View，简单起见直接使用系统提供的布局，就是一个TextView
        View view = View.inflate(viewGroup.getContext(), R.layout.recycle_pager_item, null);
        // 创建一个ViewHolder
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        Log.e("xiaxl: ", "---onBindViewHolder---");

        // 绑定数据到ViewHolder上
        viewHolder.itemView.setTag(position);
        RecyclerView.LayoutParams rl = (RecyclerView.LayoutParams) viewHolder.lv.getLayoutParams();
        if (rl == null) {
            rl = new RecyclerView.LayoutParams(mWidth, mHeight);
        } else {
            rl.width = mWidth;
            rl.height = mHeight;
        }
        viewHolder.lv.setLayoutParams(rl);
        viewHolder.itemView.setBackground(new ColorDrawable(mDataList.get(position).color));
        initLv(viewHolder.lv,mDataList.get(position).list);
    }

    private void initLv(ListView lv, ArrayList<String> list) {
        TitleAdapter titleAdapter;
        if(lv.getAdapter()==null){
            titleAdapter = new TitleAdapter(list,lv);
            lv.setAdapter(titleAdapter);
        }else{
            titleAdapter = (TitleAdapter) lv.getAdapter();
            titleAdapter.titles.clear();
            titleAdapter.titles.addAll(list);
        }
        titleAdapter.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    /**
     *
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {

        ListView lv;

        public ViewHolder(View itemView) {
            super(itemView);
            lv = itemView.findViewById(R.id.lv);
        }
    }
}
